#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > walk_polar.info <<'%EOF%'
   walk_polar
   ----------
   Molecule:         H2O
   Wave Function:    SCF
   Test Purpose:     Geometry optimization using .WALK module (2nd order),
                     with a calculation of static and dynamic
                     polarizabilities using all three different ways of
                     calculating them, vibrational analysis and Cioslowski
                     population anaylsis
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > walk_polar.mol <<'%EOF%'
ATOMBASIS
Geometry optimization using WALK module plus
static and dynamic polarizabilities
Atomtypes=2 Generators=2  X  Z
Charge=8.0 Atoms=1 Basis=6-311G Pol 1 0.0845D0 3 1.292D0 2 0.0845D0
O     0.0 -0.2249058930 0.0
Basis=6-311G Pol 1 0.0360D0 2 0.750D0 Charge=1.0 Atoms=1
H     1.45235 0.899623 0.0
H    -1.45235 0.899623 0.0
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > walk_polar.dal <<'%EOF%'
**DALTON INPUT
.WALK
**WAVE FUNCTION
.HF
**PROPERTIES
.POLARI
.VIBANA
.POPANA
.ALPHA
*ABALNR
.FREQUE
 3
0.0 0.0592 0.096
**RESPONSE
*LINEAR
.DIPLEN
.FREQUE
 2
0.0 0.0592
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >walk_polar.check
cat >>walk_polar.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -i -a"
else
   GREP="egrep -i"
fi

# Reading from atombasis:
CRIT1=`$GREP "O * 1 * 8\.0000 * 35 * 22 * \[12s6p1d\|5s4p1d\]" $log | wc -l`
CRIT2=`$GREP "H * 2 * 1\.0000 * 9 * 7 * \[6s1p\|4s1p\]" $log | wc -l`
TEST[1]=`expr $CRIT1 \+ $CRIT2`
CTRL[1]=2
ERROR[1]="ATOMBASIS NOT READ CORRECTLY"

# Reading molecular geometry:
CRIT1=`$GREP "1 * x *  0*\.0000000000" $log | wc -l`
CRIT2=`$GREP "2 * y * (\-0|\-)\.2249058930" $log | wc -l`
CRIT3=`$GREP "3 * z *  0*\.0000000000" $log | wc -l`
CRIT4=`$GREP "4 * x *  1\.4523500000" $log | wc -l`
CRIT5=`$GREP "5 * y *  0*\.8996230000" $log | wc -l`
CRIT6=`$GREP "6 * z *  0*\.0000000000" $log | wc -l`
CRIT7=`$GREP "7 * x * \-1\.4523500000" $log | wc -l`
CRIT8=`$GREP "8 * y *  0*\.8996230000" $log | wc -l`
CRIT9=`$GREP "9 * z *  0*\.0000000000" $log | wc -l`
CRIT10=`$GREP "Total number of coordinates: * 9" $log | wc -l`
TEST[2]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
              $CRIT6  \+ $CRIT7  \+ $CRIT8  \+ $CRIT9  \+ $CRIT10`
CTRL[2]=10
ERROR[2]="MOLECULAR GEOMETRY CORRECT"

# Nuclear repulsion energy:
CRIT1=`$GREP "Nuclear repulsion energy : * 9\.0550045256[0-9][0-9]" $log | wc -l`
TEST[3]=`expr $CRIT1`
CTRL[3]=1
ERROR[3]="NUCLEAR REPULSION ENERGY NOT CORRECT"

# Wave function:
#CRIT1=`$GREP "Number of closed shell electrons * 10" $log | wc -l`
#CRIT2=`$GREP "Number of electrons in active shells * 0" $log | wc -l`
#CRIT3=`$GREP " active orbitals .* 0" $log | wc -l`
#CRIT4=`$GREP "Total number of orbitals .* 36" $log | wc -l`
#CRIT5=`$GREP "Spin multiplicity .* 1" $log | wc -l`
#CRIT6=`$GREP "Total number of symmetries * 4" $log | wc -l`
#CRIT7=`$GREP "Reference state symmetry * 1" $log | wc -l`
#TEST[4]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
#              $CRIT6  \+ $CRIT7`
#CTRL[4]=7
#ERROR[4]="INCORRECT WAVE FUNCTION"

# Symmetry:
CRIT1=`$GREP "Number of coordinates in each symmetry: * 3 * 3 * 2 * 1" $log | wc -l`
CRIT2=`$GREP "Number of orbitals in each symmetry: * 17 * 11 * 6 * 2" $log | wc -l`
TEST[5]=`expr $CRIT1  \+ $CRIT2`
CTRL[5]=2
ERROR[5]="SYMMETRY IS NOT CORRECT"

# Relativistic corrections:
CRIT1=`$GREP "Darwin correction\: *           0*\.19745142.. au" $log | wc -l`
CRIT2=`$GREP "Mass\-velocity correction\: * \-0*\.2489637(5|6).. au" $log | wc -l`
CRIT3=`$GREP "Total relativistic correction\: * \-0*\.05151233.. au \(\ (0| ).0677\%\)" $log | wc -l`
CRIT4=`$GREP "Non\-relativistic \+ relativistic energy\: * \-76\.10297185.. au" $log | wc -l`
TEST[6]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[6]=4
ERROR[6]="RELATIVISTIC CORRECTIONS NOT CORRECT"

# SCF energy, 1st iteration:
CRIT1=`$GREP "Total energy * \-76\.051459519[0-9] au \(Hartrees\)" $log | wc -l`
CRIT2=`$GREP "\-2069\.4654847[0-9] eV" $log | wc -l`
CRIT3=`$GREP "\-199673\.076[0-9] kJ\/mol" $log | wc -l`
TEST[7]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[7]=3
ERROR[7]="INITIAL ENERGY NOT CORRECT"

# Gradient, 1st iteration:
CRIT1=`$GREP "O * y * (\-0|\-)\.04102" $log | wc -l`
CRIT2=`$GREP "H * x * 0*\.04951" $log | wc -l`
CRIT3=`$GREP "H * y * 0*\.04102" $log | wc -l`
TEST[8]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[8]=3
ERROR[8]="INITIAL GRADIENT NOT CORRECT"

# Hessian, 1st iteration:
# hjaaj: Hessian is too complicated to check (changes with orientation)
TEST[9]=22
CTRL[9]=22
ERROR[9]="INITIAL HESSIAN NOT CORRECT"

# Dipole moment, 1st iteration:
CRIT1=`$GREP "0*\.88703. * 2\.2546.. * 7\.5206" $log | wc -l`
TEST[10]=`expr $CRIT1`
CTRL[10]=1
ERROR[10]="INITIAL DIPOLE MOMENT NOT CORRECT"

# Step, 1st iteration:
CRIT1=`$GREP "O * 0*\.0000000000 * 0*\.0395[0-9]* * 0*\.0000000000" $log | wc -l`
CRIT2=`$GREP "H * _1 * (\-0|\-)\.0357[0-9]* * (\-0|\-)\.0197[0-9]* * 0*\.0000000000" $log | wc -l`
CRIT3=`$GREP "H * _2 * 0*\.0357[0-9]* * (\-0|\-)\.0197[0-9]* * 0*\.0000000000" $log | wc -l`
TEST[11]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3`
CTRL[11]=3
ERROR[11]="INITIAL STEP NOT CORRECT"

# Geometry, 2nd iteration:
CRIT1=`$GREP "O * 0*\.0000000000 * (\-0|\-)\.1853[0-9]* * 0*\.0000000000" $log | wc -l`
CRIT2=`$GREP "H * _1 * 1\.4165[0-9]* * 0*\.8798[0-9]* * 0*\.0000000000" $log | wc -l`
CRIT3=`$GREP "H * _2 * \-1\.4165[0-9]* * 0*\.8798[0-9]* * 0*\.0000000000" $log | wc -l`
TEST[12]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3`
CTRL[12]=6
ERROR[12]="SECOND GEOMETRY NOT CORRECT"

# SCF energy, 2nd iteration:
CRIT1=`$GREP "Total energy * \-76\.0533977[6-8].. au \(Hartrees\)" $log | wc -l`
TEST[13]=`expr $CRIT1`
CTRL[13]=1
ERROR[13]="SECOND ENERGY NOT CORRECT"

# Gradient, 2nd iteration:
CRIT1=`$GREP "O * y * 0*\.0042" $log | wc -l`
CRIT2=`$GREP "H * x * (\-0|\-)\.0068" $log | wc -l`
CRIT3=`$GREP "H * y * (\-0|\-)\.0042" $log | wc -l`
TEST[14]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[14]=3
ERROR[14]="SECOND GRADIENT NOT CORRECT"

# Hessian, 1st iteration:
# hjaaj: Hessian is too complicated to check (changes with orientation)
TEST[15]=10
CTRL[15]=10
ERROR[15]="SECOND HESSIAN NOT CORRECT"

# SCF energy, 3rd iteration:
CRIT1=`$GREP "Total energy * \-76\.053423549[0-9] au \(Hartrees\)" $log | wc -l`
TEST[16]=`expr $CRIT1`
CTRL[16]=2
ERROR[16]="THIRD ENERGY NOT CORRECT"

# SCF energy, 4th iteration:
CRIT1=`$GREP "Total energy * \-76\.053423553[0-9] au \(Hartrees\)" $log | wc -l`
#TEST[17]=`expr $CRIT1`
#CTRL[17]=2
#ERROR[17]="FOURTH ENERGY NOT CORRECT"

# Geometry optimization converged:
CRIT1=`$GREP "Norm of molecular gradient: * (0| )\.00008" $log | wc -l`
CRIT2=`$GREP "Change in energy since start of walk:(\-0| \-)\.00196403.[0-9] au" $log | wc -l`
CRIT3=`$GREP "Geometry optimization has converged." $log | wc -l`
TEST[18]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[18]=3
ERROR[18]="GEOMETRY OPTIMIZATION FAILED"

# Dipole moment gradient:
CRIT1=`$GREP "O * y * (\-0|\-)\.44820" $log | wc -l`
CRIT2=`$GREP "H * x * (\-0|\-)\.05948" $log | wc -l`
CRIT3=`$GREP "H * y * (0| )\.44820" $log | wc -l`
CRIT4=`$GREP "O * x * (\-0|\-)\.59855" $log | wc -l`
CRIT5=`$GREP "H * x * (0| )\.59855" $log | wc -l`
CRIT6=`$GREP "H * y * (\-0|\-)\.15808" $log | wc -l`
CRIT7=`$GREP "O * z * (\-0|\-)\.8091[89]" $log | wc -l`
CRIT8=`$GREP "H * z * 0*\.8091[89]" $log | wc -l`
TEST[19]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
              $CRIT6  \+ $CRIT7  \+ $CRIT8`
CTRL[19]=9
ERROR[19]="DIPOLE MOMENT GRADIENT NOT CORRECT"

# Cioslowski charges:
CRIT1=`$GREP "O      *  \-0*\.6186.." $log | wc -l`
CRIT2=`$GREP "H * _1 * (0| )\.3093.." $log | wc -l`
CRIT3=`$GREP "H * _2 * (0| )\.3093.." $log | wc -l`
TEST[20]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[20]=3
ERROR[20]="CIOSLOWSKI CHARGES NOT CORRECT"

# Static polarizabilities:
CRIT1=`$GREP "Ex * 7\.4199.. * 0*\.000000( |E\+00) * 0*\.000000" $log | wc -l`
CRIT2=`$GREP "Ey * 0*\.000000( |E\+00) * 5\.8490.. * 0*\.000000" $log | wc -l`
CRIT3=`$GREP "Ez * 0*\.000000( |E\+00) * 0*\.000000( |E\+00) * 5\.5011" $log | wc -l`
TEST[21]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[21]=6
ERROR[21]="STATIC POLARIZABILITIES NOT CORRECT"

# Dynamic polarizabilities:
CRIT1=`$GREP "Ex *  7\.4199.. * 0*\.000000 * 0*\.000000" $log | wc -l`
CRIT2=`$GREP "Ey * 0*\.000000 *  5\.8490.. * 0*\.000000" $log | wc -l`
CRIT3=`$GREP "Ez * 0*\.000000 * 0*\.000000 *  5\.5011.." $log | wc -l`
CRIT4=`$GREP "Ex *  7\.4776.. * 0*\.000000 * 0*\.000000" $log | wc -l`
CRIT5=`$GREP "Ey * 0*\.000000 *  5\.908... * 0*\.000000" $log | wc -l`
CRIT6=`$GREP "Ez * 0*\.000000 * 0*\.000000 *  5\.5761.." $log | wc -l`
CRIT7=`$GREP "Ex *  7\.5742.. * 0*\.000000 * 0*\.000000" $log | wc -l`
CRIT8=`$GREP "Ey * 0*\.000000 *  6\.0089.. * 0*\.000000" $log | wc -l`
CRIT9=`$GREP "Ez * 0*\.000000 * 0*\.000000 *  5\.7071.." $log | wc -l`
TEST[22]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
              $CRIT6  \+ $CRIT7  \+ $CRIT8  \+ $CRIT9`
CTRL[22]=12
ERROR[22]="DYNAMIC POLARIZABILITIES NOT CORRECT"

# Isotopic masses:
CRIT1=`$GREP "O * 15\.994915" $log | wc -l`
CRIT2=`$GREP "H * _1 * 1\.007825" $log | wc -l`
CRIT3=`$GREP "H * _2 * 1\.007825" $log | wc -l`
TEST[23]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[23]=6
ERROR[23]="ISOTOPIC MASSES NOT CORRECT"

# Rotational constants:
CRIT1=`$GREP "88451.\..... * 44246.\..... * 29493.\....." $log | wc -l`
CRIT2=`$GREP "29\.504... * 14\.759... * 9\.837... cm\-1" $log | wc -l`
TEST[24]=`expr $CRIT1  \+ $CRIT2`
CTRL[24]=2
ERROR[24]="ROTATIONAL CONSTANTS NOT CORRECT"

# Vibrational frequencies:
CRIT1=`$GREP "2 * A1 * 4143\.1. * 0*\.01887." $log | wc -l`
CRIT2=`$GREP "3 * A1 * 1726\.1. * 0*\.00786." $log | wc -l`
CRIT3=`$GREP "1 * B2 * 4245\.(0|1). * 0*\.01934." $log | wc -l`
TEST[25]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[25]=3
ERROR[25]="VIBRATIONAL FREQUENCIES NOT CORRECT"

# IR intensities:
CRIT1=`$GREP "2 * A1 * ....... * ........ * 25\.49. * 0*\.603(2|3)" $log | wc -l`
CRIT2=`$GREP "3 * A1 * ....... * ........ * 85\.62. * 2\.026." $log | wc -l`
CRIT3=`$GREP "1 * B2 * ....... * ........ * 88\.29. * 2\.089." $log | wc -l`
TEST[26]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[26]=3
ERROR[26]="IR INTENSITIES NOT CORRECT"

# Molecular partition function:
CRIT1=`$GREP   "50\.00 * 3\.4600(D\+|E\+)04 *    6\.07 * 1\.0000 * 2\.100...(D\+|E\+)05" $log | wc -l`
CRIT2=`$GREP  "273\.15 * 2\.4136(D\+|E\+)06 *   72\.06 * 1\.0001 * 1\.739...(D\+|E\+)08" $log | wc -l`
CRIT3=`$GREP  "298\.15 * 3\.0043(D\+|E\+)06 *   82\.06 * 1\.0002 * 2\.465...(D\+|E\+)08" $log | wc -l`
CRIT4=`$GREP "1000\.00 * 6\.1895(D\+|E\+)07 *  498\.5. * 1\.0963 * 3\.382...(D\+|E\+)10" $log | wc -l`
CRIT5=`$GREP "2000\.00 * 3\.5013(D\+|E\+)08 * 1406\.7. * 1\.5548 * 7\.658...(D\+|E\+)11" $log | wc -l`
TEST[27]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5`
CTRL[27]=5
ERROR[27]="PARTITION FUNCTION NOT CORRECT"

# Total polarizabilities
CRIT1=`$GREP "EA * 1\.119(2|3).. * (\-0|\-|0| )\.000000 * (\-0|\-|0| )\.000000" $log | wc -l`
CRIT2=`$GREP "EB * (\-0|\-|0| )\.000000 * (0| )\.9881[23]. * (\-0|\-|0| )\.000000" $log | wc -l`
CRIT3=`$GREP "EC * (\-0|\-|0| )\.000000 * (\-0|\-|0| )\.000000 * (0| )\.8151(8|9)." $log | wc -l`
TEST[28]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[28]=4
ERROR[28]="TOTAL POLARIZABILITIES NOT CORRECT"

# Check for integrals:
CRIT1=`$GREP "TRACTL\_1\: Integral transformation abandoned\," $log | wc -l`
CRIT2=`$GREP "the required MO integrals are already available\." $log | wc -l`
CRIT2b=`$GREP "Direct calculation - no two-electron integrals written or read." $log | wc -l`
TEST[29]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT2b  \+ $CRIT2b`
CTRL[29]=2
ERROR[29]="MO INTEGRALS NOT AVAILABLE"

# Linear response:
CRIT1=`$GREP "Value of linear response.*\: * 5\.8490" $log | wc -l`
CRIT2=`$GREP "Value of linear response.*\: * 5\.908(0|1)" $log | wc -l`
CRIT3=`$GREP "Value of linear response.*\: * 7\.4199" $log | wc -l`
CRIT4=`$GREP "Value of linear response.*\: * 7\.4776" $log | wc -l`
CRIT5=`$GREP "Value of linear response.*\: * 5\.5011" $log | wc -l`
CRIT6=`$GREP "Value of linear response.*\: * 5\.5761" $log | wc -l`
TEST[30]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ $CRIT6`
CTRL[30]=6
ERROR[30]="LINEAR RESPONSE NOT CORRECT"

PASSED=1
for i in 1 2 3 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20 21 22 23 24 25 \
         26 27 28 29 30
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
